using UnityEngine;
using System.Collections;

public class MallaNodos: MonoBehaviour{
	
	
	GameObject[,] vArray;
	
	public int x;
	public int y;
	public float gridSize;
	
	public float profundidad;
	public float prefabSize;
	public GameObject prefab;
	
	public bool drawLines;
	public bool visible;
	
	
	// Use this for initialization
	void Awake () {
		
		//inicializa el array. Antes estaba metido en una función aparte.
		vArray = new GameObject[x,y];
		
		
		for(int Xaxis=0;Xaxis<=vArray.GetLength(0)-1;Xaxis++)
			for(int Yaxis=0;Yaxis<=vArray.GetLength(1)-1;Yaxis++)
			{
				
				vArray[Xaxis, Yaxis] = (GameObject)Instantiate(prefab, new Vector3(Xaxis*gridSize,Yaxis*gridSize,profundidad), Quaternion.identity);
				vArray[Xaxis, Yaxis].transform.localScale = new Vector3(prefabSize,prefabSize,prefabSize);
			}
	}
	
	// Update is called once per frame
	void Update () {
		
		MeshVisibility();
	}
	
	//Dibujar el array
	void OnDrawGizmos()
	{
		if (drawLines)
		{
		for(int i=0; i<=x-1;i++)
			Gizmos.DrawLine(vArray[i,0].transform.position, vArray[i,y-1].transform.position);
		
		for(int e=0; e<=y-1;e++)
			Gizmos.DrawLine(vArray[0,e].transform.position, vArray[x-1,e].transform.position);
		}
	}
	
	void MeshVisibility()
	{
		for(int Xaxis=0;Xaxis<=vArray.GetLength(0)-1;Xaxis++)
			for(int Yaxis=0;Yaxis<=vArray.GetLength(1)-1;Yaxis++)
				vArray[Xaxis, Yaxis].renderer.enabled=visible;
	}
	
	
	/*public Nodo[,] VNodos
    {
        get { return vArray; }
       
    }*/
	
	
	
}
